﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MedReg
{
    public partial class Position : IEquatable<Position>
    {
        public void ImportList(List<Classes.Position> posList)
        {
            var mdb = new MedregDBDataContext();
            var ListPos = new List<Position>();
            foreach (var pos in posList)
            {
                Position pos1 = new Position() { ID = pos.ID, Name = pos.Name };
                Position query;

                if (mdb.Position.LongCount() == 0)
                {
                    mdb.Position.InsertOnSubmit(pos1);
                }
                else
                {
                    try
                    {
                        query = mdb.Position.Single(l => l.ID == pos1.ID);
                        query.Name = pos1.Name;

                    }
                    catch (ArgumentNullException e)
                    {
                    }
                    catch (InvalidOperationException e)
                    {

                        if (!ListPos.Contains(pos1))
                        {
                            mdb.Position.InsertOnSubmit(pos1);
                            ListPos.Add(pos1);
                        }
                    }
                }
            }
            mdb.SubmitChanges();
        }
        public bool Equals(Position other)
        {
            var i = this.ID.Equals(other.ID);
            var j = this.Name.Equals(other.Name);
            if (i & j)
            {
                return true;
            }
            return false;
        }
    }
}
